package com.lagou.config;

import com.lagou.enums.SqlCommandType;
import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

public class XMLMapperBuilder {

    private Configuration configuration;

    public XMLMapperBuilder(Configuration configuration) {
        this.configuration =configuration;
    }

//    public void parse(InputStream inputStream) throws DocumentException {
//
//        Document document = new SAXReader().read(inputStream);
//        Element rootElement = document.getRootElement();
//
//        String namespace = rootElement.attributeValue("namespace");
//
//        List<Element> list = rootElement.selectNodes("//select");
//        for (Element element : list) {
//            SqlCommandType sqlCommandType = SqlCommandType.valueOf(element.getName().toUpperCase());
//            String id = element.attributeValue("id");
//            String parameterType = element.attributeValue("parameterType");
//            String resultType = element.attributeValue("resultType");
//            String sqlText = element.getTextTrim();
//            MappedStatement mappedStatement = new MappedStatement();
//            mappedStatement.setSqlCommandType(sqlCommandType);
//            mappedStatement.setId(id);
//            mappedStatement.setParameterType(getTypeClass(parameterType));
//            mappedStatement.setResultType(getTypeClass(resultType));
//            mappedStatement.setSql(sqlText);
//            configuration.getMappedStatementMap().put(namespace + "." + id, mappedStatement);
//
//        }
//
//    }
public void parse(InputStream inputStream) throws DocumentException {
    SAXReader saxReader = new SAXReader();
    Document document = saxReader.read(inputStream);
    Element rootElement = document.getRootElement();
    String namespace = rootElement.attributeValue("namespace");

    List<Element> elements = rootElement.elements();
    for (Element element : elements) {
        SqlCommandType sqlCommandType = SqlCommandType.valueOf(element.getName().toUpperCase());
        String id = element.attributeValue("id");
        String parameterType = element.attributeValue("parameterType");
        String resultType = element.attributeValue("resultType");
        String sqlText = element.getTextTrim();
        MappedStatement mappedStatement = new MappedStatement();
        mappedStatement.setSqlCommandType(sqlCommandType);
        mappedStatement.setId(id);
        mappedStatement.setParameterType(getTypeClass(parameterType));
        mappedStatement.setResultType(getTypeClass(resultType));
        mappedStatement.setSql(sqlText);
        configuration.getMappedStatementMap().put(namespace + "." + id, mappedStatement);
    }
}
    private Class<?> getTypeClass(String typeClassName) throws DocumentException {
        if (typeClassName == null) {
            return null;
        }
        try {
            return Class.forName(typeClassName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new DocumentException(e.getMessage());
        }
    }


}
